//
//  ModifyNameViewController.m
//  parent
//
//  Created by IreWesT on 15/5/29.
//  Copyright (c) 2015年 ci123. All rights reserved.
//

#import "ChangeInterestViewController.h"
#import "AppDelegate.h"
#import "Config.h"
#import "AFAppClient.h"
#import "Tool.h"

@interface ChangeInterestViewController ()<UITextFieldDelegate>

@property (nonatomic, retain) UITextField *textField;

@end

@implementation ChangeInterestViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    _textField = [[UITextField alloc] initWithFrame:CGRectMake(10, 0, APP_SCREEN_WIDTH - 10, 40)];
    _textField.text = [[UserDao getInstance]getCurrentBaby].cname;
    _textField.returnKeyType = UIReturnKeyDone;
    _textField.delegate = self;
    _textField.text = _interest;
    _textField.clearButtonMode = UITextFieldViewModeWhileEditing;
    [self.view addSubview:_textField];
    
    CALayer *border = [CALayer layer];
    border.backgroundColor = DefaultBorderColor.CGColor;
    border.frame = CGRectMake(0, _textField.bottom, APP_SCREEN_WIDTH, .5f);
    [self.view.layer addSublayer:border];
    
    [_textField becomeFirstResponder];
    
    self.navigationItem.title = @"修改兴趣";
    
    
    UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
                                                                                    target:nil action:nil];
    negativeSpacer.width = -10;
    
    UIButton *backButton = [[UIButton alloc] initWithFrame: CGRectMake(0, 0, 40.0f, 44.0f)];
    UIImage *backImage = [[UIImage imageNamed:@"左箭头"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    [backButton setImage:backImage  forState:UIControlStateNormal];
    [backButton addTarget:self action:@selector(popBack) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *backButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    self.navigationItem.leftBarButtonItems = [NSArray arrayWithObjects: negativeSpacer, backButtonItem, nil];
}
-(void)popBack
{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    
    NSString *text = [_textField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    
    if (text.length == 0) {
        [self ShowExclaHud:@"兴趣不能为空"];
        return NO;
    }
    
    NSLog(@"%@", _textField.text);
    [_textField resignFirstResponder];
    
    [self apiChangeUserName:text];
    
    return YES;
}

#pragma mark - Networking

-(BOOL)apiChangeUserName:(NSString *)name {
    
    [super showHud:@"修改中" isDim:YES];
    
    NSString* uid = [NSString stringWithFormat:@"%d",[[Config Instance]getUID]];
    
    NSDictionary *temp_dict = @{@"uid":@([[Config Instance]getUID]),
                                @"field":@"interest",
                                @"value":name};
    [[AFAppClient sharedManager]getWithURL:[NSString stringWithFormat:@"%@%@",SERVER_URL_NEW,kMemberEditInfo] withParameters:temp_dict success:^(NSDictionary *responseObject) {
        if([[responseObject objectForKey:@"code"] integerValue]== 1){
            [self showHudComplete:@"修改成功" success:YES];
            [Tool reSetInfo];
            [self.navigationController popViewControllerAnimated:YES];
        }else{
            [self showHudComplete:@"修改失败" success:NO];
        }
    } failure:^(NSError *error) {
        [self showHudComplete:@"网络连接失败" success:NO];
    }];

   
    return YES;
}

@end
